/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Deal;
import until.HibernateUtil;

/**
 *
 * @author Admin
 */
public class dealDAO {
    //Lấy deal

    public static List<Deal> laySanPhamDeal() {
        List<Deal> ds = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "From Deal";
            Query query = sc.createQuery(hql);
            ds = query.list();
        } catch (Exception e) {
            System.err.println(e);
            sc.close();
        }
        return ds;
    }
    
    //Thêm Deal
    public static void themDeal(Deal d){
        Transaction transaction=null;
        Session sc=HibernateUtil.getSessionFactory().openSession();
        try {
            transaction=sc.beginTransaction();
            sc.save(d);
            transaction.commit();
        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
        }finally{
            sc.close();
        }
    }
    public static void capNhatDeal(Deal d){
        Transaction transaction=null;
        Session sc=HibernateUtil.getSessionFactory().openSession();
        try {
            transaction=sc.beginTransaction();
            sc.update(d);
            transaction.commit();
        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
        }finally{
            sc.close();
        }
    }
}
